//
//  ContactManager.m
//  OC6_字典&数组作业练习
//
//  Created by dllo on 15/10/30.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ContactManager.h"
#import "Contact.h"

@implementation ContactManager

- (id)init
{
    self = [super init];
    if (self) {
        [self setData:[[NSMutableDictionary alloc] init]];
    }
    return self;
}

// 3,添加联系人对象
- (void)addPerson:(Contact *)contact
{
    if (0 == [[contact name] length] || 0 == [[contact phonenum] length]) {
        NSLog(@"添加失败");
        return;
    }
    NSString *key = [[contact name] substringToIndex:1];
    NSMutableArray *arr = nil;
    if (nil == [[self data] objectForKey:key]) {
        arr = [NSMutableArray array];
        [[self data] setObject:arr forKey:key];
    }else{
        arr = [[self data] objectForKey:key];
    }
    [arr addObject:contact];
    return;
}

// 1,显示联系人信息

- (void)infoAllContact
{
    for (NSString *key in [self data]) {
        for (Contact *contact in [[self data] objectForKey:key]) {
            NSLog(@"%@ %@ %@ %@ %@ %ld", [contact name], [contact sex],[contact phonenum], [contact address], [contact group], [contact age]);
        }
    }
}




// 4, 获取某个分组名称下所有联系人,并且按照姓名升序排列

- (NSMutableArray *)sortByGroup:(NSString *)groupName
{
    
    if (0 == groupName.length) {
        return nil;
    }
    NSMutableArray *arr = [NSMutableArray array];
    for (NSString *key in [self data]) {
        for (Contact *contact in [[self data] objectForKey:key]) {
            if ([[contact group] isEqualToString:groupName]) {
                [arr addObject:contact];
            }
        }
    }
    [arr sortUsingSelector:@selector(compareContactByName:)];
    return arr;
}

// 5,从通讯录中根据电话号码搜索联系人。

- (NSMutableArray *)searchByPhone:(NSString *)phone
{
    if (0 == phone.length) {
        return nil;
    }
    NSMutableArray *arr = [NSMutableArray array];
    for (NSString *key in [self data]) {
        for (Contact *contact in [[self data] objectForKey:key]) {
            if ([contact.phonenum isEqualToString:phone]) {
                [arr addObject:contact];
            }
        }
    }
    return arr;
}


    // 6,获取所有女性的联系人，并且按照年龄的降序排列。

- (NSMutableArray *)sortByAge:(NSString *)sex
{
    if (0 == sex.length) {
        return nil;
    }
    NSMutableArray *arr =[NSMutableArray array];
    for (NSString *key in [self data]) {
        for (Contact *contact in [[self data] objectForKey:key]) {
            if ([[contact sex] isEqualTo:sex]) {
                [arr addObject:contact];
            }
        }
    }
    [arr sortUsingSelector:@selector(compareContactByAge:)];
    return arr;
}


//   7, 根据姓名删除某个联系人。


- (void)deleteByName:(NSString *)name
{
    if (0 == name.length) {
        return;
    }
    NSString *key = [name substringToIndex:1];
    NSMutableArray *arr = [[self data] objectForKey:key];
    for (NSInteger i = 0; i < arr.count; i++) {
        if ([[[arr objectAtIndex:i] name] isEqualToString:name]) {
            [arr removeObjectAtIndex:i];
            i--;
        }
    }
}


//   8, 删除某个分组的全部联系人。

- (void)deleteByGroup:(NSString *)groupName
{
    if (0 == groupName.length) {
        return;
    }
    NSMutableArray *arr = [NSMutableArray array];
    for (NSString *key in [self data]) {
        for (Contact *contact in [[self data] objectForKey:key]) {
            if ([contact.group isEqualToString:groupName]) {
                [arr addObject:contact];
            }
        }
    }
    [arr removeAllObjects];
    
//    for (NSInteger i = 0; i < [[self data] allKeys].count; i++) {
//        for (NSInteger j = 0; j < [[[self data] objectForKey:[[[self data] allKeys] objectAtIndex:i]] count]; j++) {
//            if ([[[[[self data] objectForKey:[[[self data] allKeys] objectAtIndex:i]] group] objectAtIndex:j] isEqualToString:groupName]) {
//                [[[self data] objectForKey:[[[self data] allKeys] objectAtIndex:i]] removeObjectAtIndex:j];
//            }
//        }
//    }
    
    
}






@end
